<?php

$rawTweet = "";
if (isset($_REQUEST['rawtweet']))
	$rawTweet = $_REQUEST['rawtweet'];

$redirect = "args";
if (isset($_REQUEST['redirect']))
	$redirect = $_REQUEST['redirect'];
//the args array will contain strong values like
/*
$rawTweet= stripslashes($rawTweet);

$rawTweet = $rawTweet." "; //just in case we need to "find" a space!
//echo "original: ".$rawTweet."<br />";

$moduleUrl = "";

$maxLen = strlen($rawTweet);

$bSendToTwitter = true;

for ($i=0; $i<$maxLen; $i++)
{
	if ($rawTweet[$i] == '-' && $rawTweet[$i+1] == '-')
	{

		//gotcha!
		$cmdPos = strpos($rawTweet, ':', $i);
		$argPos = $cmdPos+1;

		$cmdPos-= ($i+2);
		$commandArg = substr($rawTweet, $i+2, $cmdPos);

		if (!isKnownCommand($commandArg))
			continue;

		$paramArg = "";
		$endPos = -1;

		if ($rawTweet[$argPos]=='"')
		{
			$endPos = strpos($rawTweet, '"', $argPos+1);
			$paramArg = substr($rawTweet, $argPos+1, ($endPos-$argPos-1));
		}
		else
		{
			//need to find the next space			
			$endPos = strpos($rawTweet, ' ', $argPos);
			$paramArg = substr($rawTweet, $argPos, ($endPos-$argPos));
		}

		if ($endPos<0)
			continue;
		$paramArg = trim($paramArg);
		//echo "The Command is: ".$commandArg." with arg: ".$paramArg."<br />";
		$executeModule = $moduleUrl."?param=".urlencode($paramArg);

		$output = getModuleOutput($executeModule);


		if (strcmp(trim($output), "--nopost")==0)
		{
			//something bad happened, and we need to abort.
			$bSendToTwitter = false;
		}

		$replaceString = substr($rawTweet, $i, ($endPos-$i+1));
		$rawTweet = str_replace($replaceString, $output, $rawTweet);
		$maxLen = strlen($rawTweet);
		$i=0;
	} 
}
//echo "<br />".$rawTweet."<br />".strlen($rawTweet);
*/
include_once ("_post.php");

$rawTweet = formatTweet($rawTweet);
if (strcmp(trim($rawTweet), "--nopost")!=0)
{
	SendToTwitter($rawTweet);
}

header("Location:index.php?start=".$redirect);

	function SendToTwitter($rawTweet)
	{
		//if logged in, send, else, 
		@session_start();
		require_once('./toath/twitteroauth/twitteroauth.php');
		require_once('./toath/config.php');
		
		if (empty($_SESSION['access_token']) || empty($_SESSION['access_token']['oauth_token']) || empty($_SESSION['access_token']['oauth_token_secret']))
		{
		}
		else
		{
			$access_token = $_SESSION['access_token'];
			
			$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token['oauth_token'], $access_token['oauth_token_secret']);
			$res  = $connection->post('statuses/update', array('status' => $rawTweet));			
		}
	}
/*	function isKnownCommand($commandArg)
	{
		global $moduleUrl;
		include("components/_args.php");

		$bReturn = false;

		foreach((array)$arrCommands as $command)
		{
			if ($command[0] == $commandArg)
			{
				$moduleUrl = $command[7];
				$bReturn = true;
				break;
			}
		}

		return $bReturn;
	}
	function getModuleOutput($fvsWebUrl)
	{

		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL,$fvsWebUrl);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
		curl_setopt($ch, CURLOPT_TIMEOUT, 60);
		@session_start();
		$strCookie = 'PHPSESSID=' . $_COOKIE['PHPSESSID'] . '; path=/';

		curl_setopt($ch, CURLOPT_COOKIE, $strCookie );
		@session_write_close();
		$ret = curl_exec ($ch);

		curl_close($ch);

		return $ret;
	}
*/
?> 
